package com.cloud.animationtestdemo.strage

import android.animation.PropertyValuesHolder
import android.animation.ValueAnimator
import android.view.View

/**
 * @author: njb
 * @date:   2025/1/1 23:46
 * @desc:   描述
 */
class RotateAnimationStrategy :BaseAnimationStrategy{
    private val rotationProperty = PropertyValuesHolder.ofFloat("rotation", 0f)

    override fun getAlphaForProgress(progress: Float): Float {
        // 这里可以根据旋转进度自定义透明度变化逻辑，例如旋转角度越大越透明
        return 1 - progress * 0.3f
    }

    fun animateView(view: View, startRotation: Float, endRotation: Float, pivotX: Float, pivotY: Float, duration: Long) {
        rotationProperty.setFloatValues(startRotation, endRotation)

        val animator = ValueAnimator.ofPropertyValuesHolder(rotationProperty)
        animator.duration = duration
        animator.addUpdateListener { animation ->
            val currentRotation = (animation as? ValueAnimator)?.getAnimatedValue(rotationProperty.propertyName) as? Float?: 0f
            view.rotation = currentRotation
            val progress = animation.animatedFraction
            val alpha = getAlphaForProgress(progress)
            view.alpha = alpha
            // 设置旋转中心点
            view.pivotX = pivotX
            view.pivotY = pivotY
        }
        animator.start()
    }
}